<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
        function getJSONP({ url, params={}, callback/* =`sb${new Data()}` */ }) {
            //DOM 编程
            return new Promise((resolve, reject) => {
                let script = document.createElement('script');
                //queryString的对象？callback=show&a=1&a=2
                params = { ...params, callback };
                let arr = [];
                for (let key in params) {
                    arr.push(`${key}=${params[key]}`)
                }
                console.log(arr)
                //queryString 以'？'开始，以'&'连接
                script.src = `${url}?${arr.join('&')}`;
                window[callback] = function (data) {
                    resolve(data);
                    document.body.removeChild(script);
                }
                document.body.appendChild(script);
            })
        }
        getJSONP({
            url: 'http://localhost:3000/say',
            params: { //查询参数
                wd: '我喜欢你',
            },
            callback: 'show' //必须的
        }).then(data => {
            console.log(data);
        })
    </script>
</body>

</html>